package com.braveman.im.ui.activity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.braveman.im.R;
import com.braveman.im.bean.CardBean;
import com.braveman.im.bean.CardDeleteBean;
import com.braveman.im.manager.ApiManager;
import com.braveman.im.ui.adapter.CardAdapter;
import com.braveman.im.ui.dialog.TipsDialog;
import com.braveman.im.utils.UtilsStyle;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ht.baselibs.core.EventCode;
import com.ht.baselibs.retrofit.NetCallBack;
import com.ht.baselibs.ui.base.BaseActivity;
import com.ht.baselibs.ui.widget.TitleBarView;
import com.ht.baselibs.utils.ActivityUtils;
import com.ht.baselibs.utils.UIUtils;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.vondear.rxtools.RxBarTool;
import com.vondear.rxtools.view.dialog.RxDialogSureCancel;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Response;


/**
 * Created by 昊昊 on 2017/10/12.
 */

/**
 * 名片管理
 */
public class CardActivity extends BaseActivity {
    private RecyclerView cardView;
    private List<CardBean.DataListBean> list = new ArrayList<>();
    private CardAdapter cardAdapter;
    private TipsDialog mTipsDialog;
    public static final int REQUEST_PERMISSION_CALL = 0;
    private CardBean cardBean;
    private RxDialogSureCancel rxDialogSureCancel;
    private String deleteId;

    @Override
    protected boolean isStaticPage() {
        return true;
    }

    @Override
    protected void initTitleBar() {
        mTitleBarView.setTitleText("名片").setLeftBtnIcon(R.mipmap.back_black).addActions(new TitleBarView.Action(R.mipmap.search_icon) {
            @Override
            public void performAction(View view) {
                //搜索名片
                ActivityUtils.startActivity(CardActivity.this, SearchActivity.class);
            }
        }).addActions(new TitleBarView.Action(R.mipmap.personal_card_detail_title_icon_add) {
            @Override
            public void performAction(View view) {
                //上传名片
                ActivityUtils.startActivity(CardActivity.this, UploadABusinessCardActivity.class);
            }
        }).setOnLeftBtnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityUtils.finishActivity(CardActivity.this);
            }
        });
    }

    @Override
    protected int initContentView() {
        UtilsStyle.setStatusBarMode(this, true);
        RxBarTool.FlymeSetStatusBarLightMode(getWindow(), true);
        RxBarTool.MIUISetStatusBarLightMode(getWindow(), true);
        return R.layout.activity_mine_card;
    }

    @Override
    protected void initChildView() {
        cardView = findViewById(R.id.activity_mine_card);
        cardView.setLayoutManager(new LinearLayoutManager(this));
        cardAdapter = new CardAdapter(R.layout.item_mine_card, list);
        cardAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.tv_item_mine_card_phone_num://点击拨号
                        String personalPhone = list.get(position).personalPhone;
                        if (TextUtils.isEmpty(personalPhone)) {
                            UIUtils.showToast("电话号码为空");
                            return;
                        }
                        call(personalPhone);
                        break;
                    case R.id.tv_item_mine_card_share://点击分享
                        break;
                    case R.id.tv_item_mine_card_compile://点击编辑
                        Bundle bundle = new Bundle();
                        bundle.putString("updateid", list.get(position).id);
//                        adapter.getItemId(position);
//                        Toast.makeText(CardActivity.this, "adapter:" + adapter.toString(), Toast.LENGTH_SHORT).show();
                        Toast.makeText(CardActivity.this, "position:" + position, Toast.LENGTH_SHORT).show();
                        ActivityUtils.startActivity(CardActivity.this, CompileActivity.class, bundle);
                        break;
                }
            }
        });
        /**
         * 点击条目时间
         */
        cardAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                UIUtils.showToast("第" + position + "条");
                Bundle bundle = new Bundle();
                bundle.putString("cardId", list.get(position).id);
                ActivityUtils.startActivity(CardActivity.this, OtherDetailsActivity.class, bundle);
            }
        });
        /**
         *     长按删除事件
         */
        cardAdapter.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(BaseQuickAdapter adapter, View view, final int position) {
                //自定义弹出框选择
                rxDialogSureCancel = new RxDialogSureCancel(CardActivity.this);
                rxDialogSureCancel.getContentView().setText("是否删除该名片");
                deleteId = list.get(position).id;//获取删除ID
                rxDialogSureCancel.getSureView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //删除接口
                        ApiManager.getApi().getDelete(deleteId).enqueue(new NetCallBack<CardDeleteBean>() {
                            @Override
                            protected void onSuccess(Call<CardDeleteBean> call, Response<CardDeleteBean> response) {
                                if (response != null) {
                                    UIUtils.showToast("删除成功");
                                }
                            }
                        });
                        cardAdapter.remove(position);
                        rxDialogSureCancel.cancel();
                    }
                });
                rxDialogSureCancel.getCancelView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        rxDialogSureCancel.cancel();
                    }
                });
                rxDialogSureCancel.show();
                cardAdapter.notifyDataSetChanged();
                return false;
            }
        });

    }


    @Override
    protected void initChildData() {
        doNetWork();
    }

    private void doNetWork() {
        ApiManager.getApi().getCard(null, 1, 10).enqueue(new NetCallBack<CardBean>() {
            @Override
            protected void onSuccess(Call<CardBean> call, Response<CardBean> response) {
                if (response != null) {
                    if (list != null && list.size() > 0) {
                        list.clear();
                    }
                    cardBean = response.body();
                    list.addAll(cardBean.dataList);
                    cardView.setAdapter(cardAdapter);
                    if (cardAdapter != null) {
                        cardAdapter.notifyDataSetChanged();
                    }
                }
            }
        });
    }

    @Override
    protected void setChildViewListener() {

    }

    private void call(final String phone) {
        mTipsDialog = new TipsDialog(this);
        mTipsDialog.setTitileHint("是否拨打电话\n");
        mTipsDialog.show();
        mTipsDialog.setOnDialogTipsClickListener(new TipsDialog.OnDialogTipsClickListener() {

            @Override
            public void doEnter() {
                mTipsDialog.dismiss();
            }

            @Override
            public void doCancel() {
                Intent intent = new Intent(Intent.ACTION_CALL);
                Uri data = Uri.parse("tel:" + phone);
                intent.setData(data);
                if (ActivityCompat.checkSelfPermission(CardActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(CardActivity.this, new String[]{Manifest.permission.CALL_PHONE}, REQUEST_PERMISSION_CALL
                    );
                    return;
                }
                startActivity(intent);
                mTipsDialog.dismiss();
            }
        });
    }

    @Override
    protected boolean hasEvent() {
        return true;
    }

    @Override
    protected void onEvent(int eventCode, Object data) {
        super.onEvent(eventCode, data);
        switch (eventCode) {
            case EventCode.EVENT_UPLOAD_CARD_SUCESS:
                doNetWork();
                break;
            case EventCode.EVENT_UPDATA_CARD:
                doNetWork();
                break;
        }
    }
}
